package com.xy.exam.service.impl;

import com.xy.exam.Exception.ServiceException;
import com.xy.exam.dao.mapper.ExamIntroductionMapper;

import com.xy.exam.pojo.dto.ExamIntroductionAddNewDTO;
import com.xy.exam.pojo.dto.ExamIntroductionUpdateDTO;

import com.xy.exam.pojo.entity.ExamIntroduction;
import com.xy.exam.pojo.vo.ExamIntroductionStandardVO;
import com.xy.exam.pojo.vo.ExamIntroductionListItemVO;

import com.xy.exam.service.IExamIntroductionService;

import com.xy.exam.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ExamIntroductionServiceImpl implements IExamIntroductionService {
    @Autowired
    ExamIntroductionMapper examIntroductionMapper;
    @Override
    public void addNew(ExamIntroductionAddNewDTO examIntroductionAddNewDTO) {
        String title = examIntroductionAddNewDTO.getTitle();
        int countByTitle = examIntroductionMapper.countByTitle(title);
        if (countByTitle > 0) {
            String message = "添加考试介绍失败,该考试介绍标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        ExamIntroduction examIntroduction = new ExamIntroduction();
        examIntroduction.setUpdateTime(new Date());
        examIntroduction.setCreateTime(new Date());
        log.debug("examIntroduction:"+examIntroduction);

        BeanUtils.copyProperties(examIntroductionAddNewDTO,examIntroduction);
        log.debug("examIntroduction:"+examIntroduction);
        int insert = examIntroductionMapper.insert(examIntroduction);
        if (insert != 1) {
            String message = "添加考试介绍失败,该考试介绍标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        ExamIntroductionStandardVO standardById = examIntroductionMapper.getStandardById(id);
        if (standardById == null) {
            String message = "删除考试介绍失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        int deleteById = examIntroductionMapper.deleteById(id);
        if (deleteById !=1) {
            String message = "删除考试介绍失败，服务器忙，请稍后再尝试!";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateExamIntroductionById(Long id, ExamIntroductionUpdateDTO examIntroductionUpdateDTO) {
        ExamIntroductionStandardVO standardById = examIntroductionMapper.getStandardById(id);
        if (standardById == null) {
            String message = "修改考试介绍详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String name = examIntroductionUpdateDTO.getTitle();
        int count = examIntroductionMapper.countByTitleAndNotId(id,name);
        if (count > 0) {
            String message = "修改考试介绍详情失败，考试介绍名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        ExamIntroduction examIntroduction = new ExamIntroduction();
        examIntroduction.setUpdateTime(new Date());
        examIntroduction.setId(id);
        BeanUtils.copyProperties(examIntroductionUpdateDTO,examIntroduction);
        int rows = examIntroductionMapper.update(examIntroduction);
        if (rows != 1) {
            String message = "修改考试介绍详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public ExamIntroductionStandardVO getStandardById(Long id) {
        ExamIntroductionStandardVO standardById = examIntroductionMapper.getStandardById(id);
        if (standardById == null) {
            String message = "获取考试介绍详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return standardById;
    }

    @Override
    public List<ExamIntroductionListItemVO> list() {
        List<ExamIntroductionListItemVO> list = examIntroductionMapper.list();
        return list;
    }
}
